import request from '@/utils/request'
import type {
  RoleResponseData,
  RoleData,
  MenuResponseData,
  rolePages,
  ResponseData,
  updateRole,
} from './type'

enum API {
  ALLROLE_URL = '/sysRole/allRolesPage',
  ADDROLE_URL = '/sysRole/addRole',
  UPDATEROLE_URL = '/sysRole/UpdateRole',
  ALLPERMISSION_URL = '/sysPermission/',
  SETPERMISSION_URL = '/sysRole/UpdateRole',
  REMOVEROLE_URL = '/sysRole',
}

export const reqAllRoleList = (params: rolePages) =>
  request.get<any, RoleResponseData>(API.ALLROLE_URL, { params: params })

export const reqAddOrUpdateRole = (data: RoleData) => {
  if (data.id) {
    return request.put<any, any>(API.UPDATEROLE_URL, data)
  } else {
    return request.post<any, any>(API.ADDROLE_URL, data)
  }
}

export const reqRoleAllMenuList = (roleId: number) =>
  request.get<any, any>(API.ALLPERMISSION_URL + `${roleId}`)
export const reqAllMenuList = () => request.get<any, any>(API.ALLPERMISSION_URL)

export const reqSetPermission = (data: updateRole) =>
  request.put(API.SETPERMISSION_URL, data)

export const reqRemoveRole = (idList: number[]) =>
  request.delete<any, ResponseData>(API.REMOVEROLE_URL, { data: idList })
